package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.api.service.ApiDouyinUserService;
import com.huohuzhihui.api.service.ApiDyActivityVideoService;
import com.huohuzhihui.common.constant.Constants;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.douyin.domain.DouyinUser;
import com.huohuzhihui.douyin.service.IDouyinUserService;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerCoupon;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.domain.MerDouyin;
import com.huohuzhihui.merchant.service.IMerCouponActivityService;
import com.huohuzhihui.merchant.service.IMerCouponGetService;
import com.huohuzhihui.merchant.service.IMerCouponService;
import com.huohuzhihui.merchant.service.IMerDouyinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *微信api
 * 
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api/douyin")
public class ApiDouyinController extends BaseController
{
    @Autowired
    private ApiDouyinUserService apiDouyinUserService;
    @Autowired
    private IMerCouponGetService merCouponGetService;
    @Autowired
    private IMerCouponActivityService merCouponActivityService;
    @Autowired
    private ApiDyActivityVideoService dyActivityVideoService;
    @Autowired
    private IDouyinUserService douyinUserService;
    @Autowired
    private IMerDouyinService merDouyinService;
    @Autowired
    private IMerCouponService merCouponService;
    @Autowired
    private TokenService tokenService;

    @GetMapping("/createMerchantVideo")
    @ResponseBody
    public AjaxResult createMerchantVideo(String code,Long activityId)
    {
        //根据code查询添加顾客
        DouyinUser douyinUser = this.apiDouyinUserService.saveDouyinUserByCode(code);
        //查询营销活动
        MerCouponActivity merCouponActivity = this.merCouponActivityService.selectMerCouponActivityById(activityId);
        //发送活动视频到抖音
        dyActivityVideoService.pushVideo(merCouponActivity,douyinUser.getOpenId());

        //查询活动中的优惠券
        long couponId = merCouponActivity.getCouponId();
        merCouponGetService.receiveCoupon(douyinUser.getId(),activityId,couponId);

        MerCoupon merCoupon = merCouponService.selectMerCouponById(couponId);
        merCoupon.setRemark(douyinUser.getId()+"");
        merCoupon.setMerchantName(merCouponActivity.getMerchantName());
        return AjaxResult.success("恭喜您获得一张优惠券",merCoupon);
    }

    /**
     * 抖音商家授权
     * @param code
     * @return
     */
    @GetMapping("/deptAuth")
    @ResponseBody
    public AjaxResult deptDoAuth(String code,Long deptId)
    {
        //根据code查询添加顾客
        DouyinUser douyinUser = this.apiDouyinUserService.queryDouyinUserByCode(code);

        MerDouyin merDouyin = this.merDouyinService.selectMerDouyinByDeptId(deptId);
        if(merDouyin!=null){
            merDouyin.setOpenId(douyinUser.getOpenId());
            merDouyin.setNickname(douyinUser.getNickname());
            merDouyin.setUpdateTime(DateUtils.getNowDate());
            this.merDouyinService.updateMerDouyin(merDouyin);
        }else{
            merDouyin = new MerDouyin();
            merDouyin.setDeptId(deptId);
            merDouyin.setOpenId(douyinUser.getOpenId());
            merDouyin.setNickname(douyinUser.getNickname());
            merDouyin.setCreateTime(DateUtils.getNowDate());
            this.merDouyinService.insertMerDouyin(merDouyin);
        }
        return AjaxResult.success("授权成功");
    }


    /**
     * 查询抖音用户列表
     * @param request
     * @return
     */
    @GetMapping("/findUserList")
    @ResponseBody
    public AjaxResult findUserList(HttpServletRequest request)
    {
        LoginUser loginUser =tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();

        DouyinUser user = new DouyinUser();
        user.setMerchantId(sysUser.getDeptId());
        return AjaxResult.success("查询用户成功",this.douyinUserService.selectDouyinUserList(user));
    }
    public static void main(String[] args){
        String videoUrl = "/profile/upload/2021/03/28/8a261ccc-45e0-4c3a-82ad-67fa9d1c1e5d.mp4";
        videoUrl = videoUrl.replace(Constants.RESOURCE_PREFIX,"");
        videoUrl = "D:/ruoyi/uploadPath" + videoUrl;
        System.out.println(videoUrl);
    }




}
